package Simpleblog::Controller::Signup;

use strict;
use warnings;
use base 'Catalyst::Controller';

=head1 NAME

Simpleblog::Controller::Signup - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 signup

signup process

=cut

sub signup : Global {
    my ( $self, $c ) = @_;

    # Create the widget
    my $w = $self->make_signup_widget($c);

    $c->stash->{widget_result} = $w->result;

    # Set the template
    $c->stash->{template} = 'signup.tt2';

    return;

}


=head2 do_signup

do signup work

=cut

sub do_signup : Local {
    my ( $self, $c ) = @_;

    # Create the widget
    my $w = $self->make_signup_widget($c);

    # Validate the form parameters
    my $result = $w->process($c->req);

    # Retrieve the data from the form
    my $name = $result->params->{name};
    my $password = $result->params->{password};
    my $email = $result->params->{email};
    
    # Write form (including validation error messages) to
    # stash variable for use in template
    $c->stash->{widget_result} = $result;

    # Warn the user at the top of the form that there were errors.
    # Note that there will also be per-field feedback on
    # validation errors because of '$w->process($c->req)' above.
    $c->stash->{error_msg} = 'Validation errors!';

    # Set the template
    $c->stash->{template} = 'signup.tt2';

    # Were their validation errors?
    if ( $result->has_errors) {
        return;
    } 

    
    # Everything validated OK, so do the create 
    # user

    my $user_record = {
        name => $name,
        password => $password,
        email => $email
    };

    if ($c->model('Simpleblog::Users')->create($user_record)) {
        $c->login($name, $password);
        $c->res->redirect("/$name");

        return;
    }
    else {
        $result->add_error({
            name => 'name',
            message => 'This name has been used!',
        });
    }


}



=head2 make_signup_widget

Build an HTML::Widget form for signup

=cut

sub make_signup_widget {
    my ($self, $c) = @_;

    # Create an HTML::Widget to build the form
    my $w = $c->widget('signup_form')->method('post');
    $w->action($c->uri_for('do_signup'));

    # Create the form feilds
    my $name = $w->element('Textfield', 'name'  );
    $name->label('Username')->size(10);
    $name->value($c->stash->{name});

    my $password = $w->element('Password', 'password'  );
    $password->label('Password')->size(10);

    my $rpassword = $w->element('Password', 'rpassword'  );
    $rpassword->label('Retype Password')->size(10);

    my $email = $w->element('Textfield', 'email'  );
    $email->value($c->stash->{email});
    $email->label('Email')->size(10);

    my $captcha = $w->element('Textfield', 'captcha');
    $captcha->label('Check code');
    
    my $captcha_img = $w->element('Block');
    $captcha_img->type('img');
    $captcha_img->attributes(src=>'/utils/captcha');
    

    $w->element('Submit', 'submit' )->value('submit');

    $w->constraint(All     => qw/username password rpassword email/)
        ->message('Required. ');

    $w->constraint(Length  => qw/name/)->min(5)->max(32)
        ->message('Must be between 5 and 32 characters. ');

    $w->constraint( 'Email' => 'email' )->message('Must be a valid email address!');

    # check whether user name has exist
    my $check_user_exists = sub {
        my $name = shift;
        # return true if user not exist, else false
        return not $c->model('Simpleblog::Users')->count({name => $name});
    };
    
    
    $w->constraint(Callback => 'name')->callback($check_user_exists)
        ->message('This name has been used!');

    my $constraint = $w->constraint('Equal' => qw/password rpassword/);
    $constraint->render_errors( 'rpassword' )->message('Must be same with password');

    $w->constraint('String' => 'name')->message('Must be alphanumberic!');

    my $check_code = sub {
        my $code = shift;
        return $c->validate_captcha($code);
    };
    $w->constraint(Callback => 'captcha')->callback($check_code)
           ->message('Check code wrong!');

        
    # Return the widget
    return $w;
}



=head1 AUTHOR

chylli,,,

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
